import prefixedDispatch from './prefixedDispatch'

export function run (subs, model, app, onError) {
    const funcs = []
    const noFuncs = []

    for (const key in subs) {
        if (Object.prototype.hasOwnProperty.call(subs, key)) {
            const sub = subs[key]
            const unlistener = sub({
                dispatch: prefixedDispatch(app._sotre.dispatch, model),
                history: app._history
            }, onError)

            if (isFunction(unlistener)) {
                funcs.push(unlistener)
            } else {
                noFuncs.push(key)
            }
        }

        return { funcs, noFuncs }
    }
}

export function unlisten (unlisteners, namespace) {
    if (!unlisteners[namespace]) {
        return
    }

    const { funcs, noFuncs } = unlisteners[namespace]
    
    for (const unlistener of funcs) {
        unlistener()
    }
    delete unlisteners[namespace]
}